<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <section>
        <input type="text" placeholder="输入评论">
        <button>发布</button>
    </section>
    <script>
        // 获取元素
        const input = document.querySelector("input")
        const button = document.querySelector("button")

        // 回车的事件处理函数
        function enterHandler(e) {
            if (e.key === "Enter") {
                const div = document.createElement("div")
                const body = document.body
                div.innerHTML = e.target.value
                body.append(div)
                e.target.value = ""
            }
        }

        // 点击的事件处理函数
        function clickHandler(e) {
                const div = document.createElement("div")
                const body = document.body
                div.innerHTML = input.value
                body.append(div)
                input.value = ""
        }
  

        input.addEventListener("keyup", function(e){
            enterHandler(e)
        })
        button.addEventListener("click", function (e) {
          clickHandler(e)
        })
    </script>
</body>

</html>